package com.cxs.app.cptlib;

import com.cxs.app.enums.IMIconType;
import com.cxs.app.util.Constants;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 自定义图标组件
 * 继承自基础组件Text
 */
public class IMIcon extends Text {
    public IMIcon(Context context) {
        super(context);
    }

    public IMIcon(Context context, AttrSet attrSet) {
        super(context, attrSet);
        setTypeFace(context);
        initIMIcon(attrSet);
    }

    /**
     * icon 使用的font awesome
     * @param attrSet
     */
    private void initIMIcon(AttrSet attrSet) {
        // 设置图标居中
        setTextAlignment(TextAlignment.CENTER);
        // 设置图标大小
        // getAttr(str)是从xml文件的属性中获取名称为str的value值
        if (attrSet.getAttr("icon_size").isPresent()) {
            setTextSize(attrSet.getAttr("icon_size").get().getIntegerValue(), TextSizeType.FP);
        } else {
            setTextSize(Constants.DEFAULT_ICON_WIDTH_HEIGHT, TextSizeType.FP);
        }

        // 设置图标内容，使用Unicode编码方式，我们自定义的Font样式
        // getAttr(str)是从xml文件的属性中获取名称为str的value值
        if (attrSet.getAttr("icon").isPresent()) {
            String icon = attrSet.getAttr("icon").get().getStringValue();
            setText(IMIconType.valueOf(icon).getStyle());
        } else {
            setText(IMIconType.USER.getStyle());
        }
    }

    /**
     * 读取字体文件，并设置为自定义组件的字体
     * @param context
     */
    private void setTypeFace(Context context) {
        String typeFace = "fontawesome-webfont.ttf";
        File file = new File(context.getCodeCacheDir(), typeFace);

        OutputStream outputStream = null;

        ResourceManager resManager = context.getResourceManager();

        RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/fonts/" + typeFace);

        Resource resource = null;
        try {
            resource = rawFileEntry.openRawFile();
            outputStream = new FileOutputStream(file);
            int index;
            byte[] bytes = new byte[1024];
            if (resource != null) {
                while ((index = resource.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, index);
                    outputStream.flush();;
                }
                // 使用HarmonyOS提供的Font类构建Text类使用的Font
                Font font = new Font.Builder(file).build();
                // 调用父类的setFont方法设置字体
                setFont(font);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                resource.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

